//
//  UIScrollView+EmptyView.m
//  RaidCall
//
//  Created by rambo on 2018/1/9.
//  Copyright © 2018年 RaidCall. All rights reserved.
//

#import "UIScrollView+EmptyView.h"
#import "NSObject+HBCategory.h"
#import <objc/message.h>
#import <objc/runtime.h>

static void *kEmptyViewKey = &kEmptyViewKey;
static void *kAutoShowEmptyKey = &kAutoShowEmptyKey;

@implementation UIScrollView (EmptyView)

+ (void)load{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(initWithFrame:) newSel:@selector(EmptyView_initWithFrame:)];
    });

}

- (instancetype)EmptyView_initWithFrame:(CGRect)frame{
//    id view = [self EmptyView_initWithFrame:frame];
    if ([self EmptyView_initWithFrame:frame]) {
        self.emptyView_autoShowEmpty = YES;
    }
    return self;
}

- (void)setHb_emptyView:(HBScrollEmptyView *)hb_emptyView{
    if (self.hb_emptyView != hb_emptyView) {
        objc_setAssociatedObject(self, kEmptyViewKey, hb_emptyView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        for (UIView *subview in self.subviews) {
            if (![subview isKindOfClass:[HBScrollEmptyView class]]) continue;
            [subview removeFromSuperview];
        }
        [self addSubview:self.hb_emptyView];
    }
}


- (HBScrollEmptyView *)hb_emptyView{
    return objc_getAssociatedObject(self, kEmptyViewKey);
}


- (NSUInteger)dataSourceTotalCount{
    NSUInteger count = 0;
    
    if ([self isKindOfClass:[UITableView class]]) {
        UITableView *tableView = (UITableView*)self;
        for (NSInteger idx = 0 ; idx < [tableView numberOfSections]; idx++) {
            count += [tableView numberOfRowsInSection:idx];
        }
    }else if ([self isKindOfClass:[UICollectionView class]]){
        UICollectionView *collectionView = (UICollectionView*)self;
        for (NSInteger idx = 0 ; idx < [collectionView numberOfSections]; idx++) {
            count += [collectionView numberOfItemsInSection:idx];
        }
    }
    
    return count;
}


- (void)emptyView_hiddenEmpty{
    if (self.hb_emptyView) {
        self.hb_emptyView.hidden = YES;
    }
}

- (void)emptyView_showEmpty{
    if (self.hb_emptyView && self.emptyView_autoShowEmpty) {
        self.hb_emptyView.hidden = NO;
        [self bringSubviewToFront:self.hb_emptyView];
    }
}

- (void)emptyView_startLoadingData{
    if (self.hb_emptyView) {
        self.hb_emptyView.hidden = YES;
    }
}

- (void)emptyView_getDataReload{
    if (!self.hb_emptyView) return;
    if ([self dataSourceTotalCount] > 0) {
        [self emptyView_hiddenEmpty];
    }else{
        [self emptyView_showEmpty];
    }
}

- (void)emptyView_endLoadData{
    if (self.hb_emptyView && !self.emptyView_autoShowEmpty) {
        if ([self dataSourceTotalCount] > 0) {
            [self emptyView_hiddenEmpty];
            
        }else{
            self.hb_emptyView.hidden = NO;
            [self bringSubviewToFront:self.hb_emptyView];
        }
    }
}

- (void)setEmptyView_autoShowEmpty:(BOOL)emptyView_autoShowEmpty{
    objc_setAssociatedObject(self, kAutoShowEmptyKey, @(emptyView_autoShowEmpty), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)emptyView_autoShowEmpty{
    return [objc_getAssociatedObject(self, kAutoShowEmptyKey) boolValue];
}

@end




@implementation UITableView (EmptyView)

+ (void)load{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(reloadData) newSel:@selector(EmptyView_reloadData)];
        
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(insertSections:withRowAnimation:) newSel:@selector(EmptyView_insertSections:withRowAnimation:)];
        
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(deleteSections:withRowAnimation:) newSel:@selector(EmptyView_deleteSections:withRowAnimation:)];
        
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(insertRowsAtIndexPaths:withRowAnimation:) newSel:@selector(EmptyView_insertRowsAtIndexPaths:withRowAnimation:)];
        
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(deleteRowsAtIndexPaths:withRowAnimation:) newSel:@selector(EmptyView_deleteRowsAtIndexPaths:withRowAnimation:)];
    });

}

- (void)EmptyView_reloadData{
    [self EmptyView_reloadData];
    [self emptyView_getDataReload];
}


- (void)EmptyView_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation{
    [self EmptyView_insertSections:sections withRowAnimation:animation];
    [self emptyView_getDataReload];
}

- (void)EmptyView_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation{
    [self EmptyView_deleteSections:sections withRowAnimation:animation];
    [self emptyView_getDataReload];
}

- (void)EmptyView_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation{
    [self EmptyView_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
    [self emptyView_getDataReload];
}

- (void)EmptyView_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation{
    [self EmptyView_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
    [self emptyView_getDataReload];
}


@end


@implementation UICollectionView (EmptyView)

+ (void)load{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(reloadData) newSel:@selector(EmptyView_reloadData)];
        
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(reloadSections:) newSel:@selector(EmptyView_reloadSections:)];
        
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(reloadItemsAtIndexPaths:) newSel:@selector(EmptyView_reloadItemsAtIndexPaths:)];
        
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(insertSections:) newSel:@selector(EmptyView_insertSections:)];
        
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(deleteSections:) newSel:@selector(EmptyView_deleteSections:)];
        
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(insertItemsAtIndexPaths:) newSel:@selector(EmptyView_insertItemsAtIndexPaths:)];
        
        [NSObject hb_swizzlingInstanceMethodWithClass:self originSel:@selector(deleteItemsAtIndexPaths:) newSel:@selector(EmptyView_deleteItemsAtIndexPaths:)];
    });

}

- (void)EmptyView_reloadData{
    [self EmptyView_reloadData];
    [self emptyView_getDataReload];
}

- (void)EmptyView_insertSections:(NSIndexSet *)sections{
    [self EmptyView_insertSections:sections];
    [self emptyView_getDataReload];
}
- (void)EmptyView_deleteSections:(NSIndexSet *)sections{
    [self EmptyView_deleteSections:sections];
    [self emptyView_getDataReload];
}
- (void)EmptyView_reloadSections:(NSIndexSet *)sections{
    [self EmptyView_reloadSections:sections];
    [self emptyView_getDataReload];
}

- (void)EmptyView_insertItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths{
    [self EmptyView_insertItemsAtIndexPaths:indexPaths];
    [self emptyView_getDataReload];
}
- (void)EmptyView_deleteItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths{
    [self EmptyView_deleteItemsAtIndexPaths:indexPaths];
    [self emptyView_getDataReload];
}
- (void)EmptyView_reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths{
    [self EmptyView_reloadItemsAtIndexPaths:indexPaths];
    [self emptyView_getDataReload];
}

@end


